package com.example.entity;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Data
public class User implements UserDetails {

    //用户基本信息
    private String Num;
    private Role role;
    private String Name;
    private String Phone;
    private String Password;
    private String LoginTime;
    private Integer Del;
    private String Sex;

    public User(){

    }
    public User(String num,Role role){
        this.Num = num;
        this.role = role;
    }
    public User(String num,String name,String phone,String sex, String password,String loginTime,Integer del){
        this.Num=num;
        this.Name=name;
        this.Sex=sex;
        this.Phone=phone;
        this.Password = new BCryptPasswordEncoder().encode(password);
        this.LoginTime=loginTime;
        this.Del=del;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        GrantedAuthority authority=new SimpleGrantedAuthority(this.getRole().getROLE_name());
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(authority);
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.Password;
    }

    public void setPassword(String password) {
        this.Password = new BCryptPasswordEncoder().encode(password);
    }

    @Override
    public String getUsername() {
        return this.getNum();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public boolean equals(Object obj) {
        //会话并发生效，使用num判断是否是同一个用户
        if (obj instanceof User){
            //字符串的equals方法是已经重写过的
            return ((User) obj).getNum().equals(this.getNum());
        }else {
            return false;
        }
    }

}